/**
 * 
 */
package com.mevoy.async.dao.couchdb;

import java.util.List;

import org.apache.log4j.Logger;
import org.jcouchdb.db.Database;
import org.jcouchdb.db.Server;
import org.jcouchdb.db.ServerImpl;

/**
 * @author diegoicosta
 * 
 */
public class CouchDBConnection {
	Logger logger = Logger.getLogger(this.getClass());

	private String couchdbHost;
	private int couchdbPort;
	private String couchdbName;
	private Database dataBase;

	/*
	 * METODOS PUBLICOS
	 */

	public void setCouchdbHost(String couchdbHost) {
		this.couchdbHost = couchdbHost;
	}

	public void setCouchdbPort(int couchdbPort) {
		this.couchdbPort = couchdbPort;
	}

	public void setCouchdbName(String couchdbName) {
		this.couchdbName = couchdbName;
	}

	public Database getCouchDB() {
		if (dataBase == null) {
			Server server = new ServerImpl(couchdbHost, couchdbPort);
			List<String> databases = server.listDatabases();
			logger.debug("databases = " + databases);

			if (databases.contains(couchdbName)) {
				dataBase = new Database(couchdbHost, couchdbPort, couchdbName);
			}
			server.createDatabase(couchdbName);
		}
		return dataBase;
	}

}
